<template>
  <label class="yd-checkbox" :class="{'is-checked': isChecked}">
    <span class="checkbox-icon">
      <span class="checkbox-inner"></span>
      <input
        class="checkbox-original"
        type="checkbox"
        :name="name"
        :value="value"
        v-model="myModel"
      />
    </span>
    <span class="checkbox-label">
      <slot>{{label}}</slot>
    </span>
  </label>
</template>

<script>
import { ref, computed, getCurrentInstance, inject } from "vue";
export default {
  name: "yd-checkbox",
  props: {
    label: {
      type: String,
      default: ""
    },
    value: null,
    name: null,
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  setup(props, ctx) {
    const group = getCurrentInstance().parent;

    const isInGroup = computed(() => {
      return Boolean(group.vnode.type.name === "yd-checkbox-group");
    });
    const isChecked = computed(() => {
      return isInGroup.value
        ? group.ctx.modelValue.includes(props.value)
        : props.modelValue;
    });
    const myModel = computed({
      get() {
        return isInGroup.value ? group.ctx.modelValue : props.modelValue;
      },
      set(val) {
        return isInGroup.value
          ? group.emit("update:modelValue", val)
          : ctx.emit("update:modelValue", !props.modelValue);
      }
    });

    return {
      myModel,
      isChecked
    };
  }
};
</script>

<style lang="scss" scoped>
.yd-checkbox {
  color: #606266;
  font-weight: 500;
  font-size: 14px;
  position: relative;
  cursor: pointer;
  display: inline-block;
  white-space: nowrap;
  user-select: none;
  margin-right: 30px;
  .checkbox-icon {
    white-space: nowrap;
    cursor: pointer;
    outline: none;
    display: inline-block;
    line-height: 1;
    position: relative;
    vertical-align: middle;
    .checkbox-inner {
      display: inline-block;
      position: relative;
      border: 1px solid #dcdfe6;
      border-radius: 2px;
      box-sizing: border-box;
      width: 14px;
      height: 14px;
      background-color: #fff;
      z-index: 1;
      transition: border-color 0.25s cubic-bezier(0.71, -0.46, 0.29, 1.46),
        background-color 0.25s, cubic-bezier(0.71, -0.46, 0.29, 1.46);
      &::after {
        box-sizing: content-box;
        content: "";
        border: 1px solid #ffffff;
        border-left: 0;
        border-top: 0;
        width: 3px;
        height: 7px;
        left: 4px;
        position: absolute;
        top: 1px;
        transform: rotate(45deg) scaleY(0);
        transition: transform 0.15s ease-in 0.05s;
        transform-origin: center;
      }
    }
  }
  .checkbox-original {
    opacity: 0;
    outline: none;
    position: absolute;
    left: 10px;
    margin: 0;
    width: 0;
    height: 0;
    z-index: -1;
  }
  .checkbox-label {
    display: inline-block;
    padding-left: 10px;
    line-height: 19px;
    font-size: 14px;
  }
  &.is-checked {
    .checkbox-icon {
      .checkbox-inner {
        background-color: #409eff;
        border-color: #409eff;
        &::after {
          transform: rotate(45deg) scaleY(1);
        }
      }
    }
    .checkbox-label {
      color: #409eff;
    }
  }
}
</style>